package com.dyzx.hbb.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.sys.entity.SysDept;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.query.DeptQuery;
import com.dyzx.hbb.modules.sys.service.SysDeptService;
import com.dyzx.hbb.modules.sys.vo.DeptUVO;
import com.dyzx.hbb.modules.sys.vo.DeptVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/sys/dept")
@RestController
@Tag(name = "04.部门管理", description = "部门相关接口")
public class DeptController extends BaseController<SysDeptService, SysDept, DeptQuery, DeptVO, DeptUVO> {

    @Autowired
    private SysDeptService service;



    @Override
    protected SysDeptService getBaseService() {
        return service;
    }



    @Override
    protected MyLambdaQueryWrapper<SysDept> queryWrapper(DeptQuery query) {
        MyLambdaQueryWrapper<SysDept> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysDept::getId, SysDept::getName, SysDept::getLeader, SysDept::getLeaderPhone, SysDept::getStatus,SysDept::getCreateDate, SysDept::getUpdateDate);
        wrapper.organName(SysDept::getOrganId).as("organ_name");
        if (query != null) {
            Long organId = SecurityContextHolder.getOrganId();

            wrapper.like(StringUtils.isNotBlank(query.getName()), SysDept::getName, query.getName())
                    .like(StringUtils.isNotBlank(query.getLeader()), SysDept::getLeader, query.getLeader())
                    .inSet(query.getStatus() != null, SysDept::getStatus, query.getStatus())
                    .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysDept::getCreateDate, DateFormat.DATE, query.getCreateDate())
                    .like(StringUtils.isNotBlank(query.getLeaderPhone()), SysDept::getLeaderPhone, query.getLeaderPhone()).eq(SecurityContextHolder.isNotAdmin(), SysDept::getOrganId, organId);

        }

        return wrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<SysDept> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysDept> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysDept::getId, SysDept::getName, SysDept::getLeader, SysDept::getLeaderPhone, SysDept::getStatus,SysDept::getCreateDate, SysDept::getUpdateDate);
       // wrapper.select(SysDept::getId, SysDept::getName, SysDept::getLeader, SysDept::getLeaderPhone, SysDept::getStatus,SysDept::getCreateDate, SysDept::getUpdateDate);
        wrapper.organName(SysDept::getOrganId).as("organ_name");
        return wrapper;
    }

    @Operation(summary = "获取机构简易列表")
    @GetMapping("/mini_list")
    public Result<List<Map<String, Object>>> miniList() {
        Long organId = SecurityContextHolder.getOrganId();
        MyLambdaQueryWrapper<SysDept> wrapper =new MyLambdaQueryWrapper<>();
        wrapper.select(SysDept::getId, SysDept::getName).eq(SysDept::getOrganId, organId);
        wrapper.select(SysDept::getName).as("label").select(SysDept::getId).as("value");
        return Result.ok(service.listMaps(wrapper));
    }


}
